﻿using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Component.Mesh
{
    [Serializable]
    public class LineData
    {
    
        public List<Vector2> Points { get; set; } = new List<Vector2>();
        public List<Vector2> TurnPoint { get; set; } = new List<Vector2>();
    
        public Vector2 StartPoint { get; set; }
        public Color LineColor { get; set; }

        public LineData()
        {
        
        }
    
        public LineData(Vector2[] p, Vector2 sp, Vector2 tp)
        {
            Points.AddRange(p.Reverse());
            StartPoint = sp;
            TurnPoint.Add(tp);
        }

        /// <summary>
        /// 获取落点在线中心的方向
        /// </summary>
        /// <param name="point">落点</param>
        /// <returns>方向</returns>
        public Vector2 PointOnLine(Vector2 point)
        {
            int directionX = 0;
            int directionY = 0;

            int index = 0;
        
            foreach(Vector2 v in TurnPoint)
            {
                // if(v.d)
            
            }
        
            // Vector2 distance = point - TurnPoint;

            // if (Math.Abs(distance.x) < Mathf.Abs(distance.y))
            // {
            //     directionY = point.y < TurnPoint.y ? -1 : 1;
            // }
            // else
            // {
            //     directionX = point.x < TurnPoint.x ? -1 : 1;
            // }
        
            return new Vector2(directionX, directionY);
        }

        public static LineData operator +(LineData a, LineData b)
        {
            if (a == null)
            {
                a = new LineData();
            }
            if (a.Points.Count == 0)
            {
                a.Points.AddRange(b.Points);
                return a;
            }

            List<Vector2> temp = new List<Vector2>();
            if (a.Points[0] == b.Points[0])
            {
                b.Points.Reverse();
                temp.AddRange(b.Points.ToArray());
                foreach (var p in a.Points)
                {
                    if (!temp.Contains(p))
                    {
                        temp.Add(p);
                    }
                }
            }else if (a.Points[0] == b.Points[b.Points.Count - 1])
            {
                temp.AddRange(b.Points);
                temp.AddRange(a.Points);
            }else if (a.Points[a.Points.Count - 1] == b.Points[0])
            {
                temp.AddRange(a.Points);
                foreach (var p in b.Points)
                {
                    if (!temp.Contains(p))
                    {
                        temp.Add(p);
                    }
                }
            }
            a.Points = temp;
            return a;
        }

        public static LineData operator -(LineData a, LineData b)
        {
            for (int i = 0; i < a.Points.Count; i++)
            {
                if (a.Points[i] == b.Points[0])
                {
                    var startIndex = i;
                    Debug.Log(b.Points[0] + " : " + b.Points[1]);
                    if (a.Points[startIndex + b.Points.Count - 1] == b.Points[b.Points.Count - 1])
                    {
                        a.Points.RemoveRange(startIndex == 0 ? startIndex : startIndex + 1, b.Points.Count - 1);
                    }
                    else
                    {
                        a.Points.RemoveRange(startIndex - b.Points.Count + 1, b.Points.Count);
                    }
                }
            }
            return a;
        }
    
    
    }
}